clear all;

z=1.75;
departure=1;
destination=2;
kappa=2.2;
x=[5,8];

cd ..; cd ..; cd ..;
load('code_main_model_eta_0\6_implied_w_r\computation_results\data.mat','V_ini','gb_ini','gtheta_ini','gridb','gridtheta','gridz','nmkt','nb','nz','ntheta','eta_ini');
zindex=find(gridz==z);
V_low=squeeze(V_ini(1,:,zindex,:,:));
gb_low=squeeze(gb_ini(:,zindex,:,:));
gtheta_low=squeeze(gtheta_ini(:,zindex,:,:));
[V_low_not_mig, V_low_mig]=value(V_low, gb_low, gtheta_low, gridb, gridtheta, nb, ntheta, departure, destination, kappa);
eta_low=0;

cd ..; cd ..; cd ..;
load('code_main_model_eta_1\6_implied_w_r\computation_results\data.mat','V_ini','gb_ini','gtheta_ini','gridb','gridtheta','gridz','nmkt','nb','nz','ntheta','eta_ini');
cd 'code_figure_table\OA\Figure OA22';zindex=find(gridz==z);
V_mid=squeeze(V_ini(1,:,zindex,:,:));
gb_mid=squeeze(gb_ini(:,zindex,:,:));
gtheta_mid=squeeze(gtheta_ini(:,zindex,:,:));
[V_mid_not_mig, V_mid_mig]=value(V_mid, gb_mid, gtheta_mid, gridb, gridtheta, nb, ntheta, departure, destination, kappa);
eta_mid=eta_ini;

cd ..; cd ..; cd ..;
load('code_main_model_eta_2\6_implied_w_r\computation_results\data.mat','V_ini','gb_ini','gtheta_ini','gridb','gridtheta','gridz','nmkt','nb','nz','ntheta','eta_ini');
cd 'code_figure_table\OA\Figure OA22';zindex=find(gridz==z);
V_high=squeeze(V_ini(1,:,zindex,:,:));
gb_high=squeeze(gb_ini(:,zindex,:,:));
gtheta_high=squeeze(gtheta_ini(:,zindex,:,:));
[V_high_not_mig, V_high_mig]=value(V_high, gb_high, gtheta_high, gridb, gridtheta, nb, ntheta, departure, destination, kappa);
eta_high=eta_ini;

theta=0.8;
theta_index=find(gridtheta==theta);
bindex=1:1:nb;

x1=gridb(bindex)';
y1=squeeze(V_low_mig(bindex,theta_index));
x2=gridb(bindex)';
y2=squeeze(V_mid_mig(bindex,theta_index));
x3=gridb(bindex)';
y3=squeeze(V_high_mig(bindex,theta_index));

%% draw
figure('color',[1,1,1],'position',[20,40,1200,720]);

subplot(2,3,1);
plot(x1,y1,'-b','linewidth',2);
hold on
plot(gridb(bindex),squeeze(V_low_not_mig(bindex,theta_index)),'--k','linewidth',2);
hold on
plot(x1,y1-eta_low/0.05*0.1831,'--b','linewidth',1);
hold on
plot(x1,y1+eta_low/0.05*0.1831,'--b','linewidth',1);
legend('migrate','not migrate','interpreter','Latex','location','northwest')
title('A. Markets with branches, $\eta=0$', 'FontSize',15,'Interpreter','Latex');
xlabel('total wealth $m_t+a_t$', 'FontSize',12,'Interpreter','Latex');
ylabel('household''s value at $t+1$', 'FontSize',12,'Interpreter','Latex');
xlim(x);
xticks(5:1:8)
%ylim([-7,-4])
yticks(-7:1:-4)
set(gca,'FontSize',12);

subplot(2,3,2);
plot(x2,y2,'-b','linewidth',2);
hold on
plot(x2,y2-eta_mid/0.05*0.1831,'--b','linewidth',1);
hold on
plot(x2,y2+eta_mid/0.05*0.1831,'--b','linewidth',1);
hold on
plot(gridb(bindex),squeeze(V_mid_not_mig(bindex,theta_index)),'--k','linewidth',2);
title('B. Markets with branches, $\eta=0.1$', 'FontSize',15,'Interpreter','Latex');
xlabel('total wealth $m_t+a_t$', 'FontSize',12,'Interpreter','Latex');
ylabel('household''s value at $t+1$', 'FontSize',12,'Interpreter','Latex');
xlim(x);
xticks(5:1:8)
%ylim([-7,-4])
yticks(-7:1:-4)
set(gca,'FontSize',12);

subplot(2,3,3);
plot(x3,y3,'-b','linewidth',2);
hold on
plot(x3,y3-eta_high/0.05*0.1831,'--b','linewidth',1);
hold on
plot(x3,y3+eta_high/0.05*0.1831,'--b','linewidth',1);
hold on
plot(gridb(bindex),squeeze(V_high_not_mig(bindex,theta_index)),'--k','linewidth',2);
title('C. Markets with branches, $\eta=0.2$', 'FontSize',15,'Interpreter','Latex');
xlabel('total wealth $m_t+a_t$', 'FontSize',12,'Interpreter','Latex');
ylabel('household''s value at $t+1$', 'FontSize',12,'Interpreter','Latex');
xlim(x);
xticks(5:1:8)
%ylim([-7,-4])
yticks(-7:1:-4)
set(gca,'FontSize',12);






% clear all;

z=1.75;
departure=2;
destination=1;
kappa=2.2;

cd ..; cd ..; cd ..;
load('code_main_model_eta_0\6_implied_w_r\computation_results\data.mat','V_ini','gb_ini','gtheta_ini','gridb','gridtheta','gridz','nmkt','nb','nz','ntheta','eta_ini');
cd 'code_figure_table\OA\Figure OA22';
zindex=find(gridz==z);
V_low=squeeze(V_ini(1,:,zindex,:,:));
gb_low=squeeze(gb_ini(:,zindex,:,:));
gtheta_low=squeeze(gtheta_ini(:,zindex,:,:));
[V_low_not_mig, V_low_mig]=value(V_low, gb_low, gtheta_low, gridb, gridtheta, nb, ntheta, departure, destination, kappa);
eta_low=0;

cd ..; cd ..; cd ..;
load('code_main_model_eta_1\6_implied_w_r\computation_results\data.mat','V_ini','gb_ini','gtheta_ini','gridb','gridtheta','gridz','nmkt','nb','nz','ntheta','eta_ini');
cd 'code_figure_table\OA\Figure OA22';
zindex=find(gridz==z);
V_mid=squeeze(V_ini(1,:,zindex,:,:));
gb_mid=squeeze(gb_ini(:,zindex,:,:));
gtheta_mid=squeeze(gtheta_ini(:,zindex,:,:));
[V_mid_not_mig, V_mid_mig]=value(V_mid, gb_mid, gtheta_mid, gridb, gridtheta, nb, ntheta, departure, destination, kappa);
eta_mid=eta_ini;

cd ..; cd ..; cd ..;
load('code_main_model_eta_2\6_implied_w_r\computation_results\data.mat','V_ini','gb_ini','gtheta_ini','gridb','gridtheta','gridz','nmkt','nb','nz','ntheta','eta_ini');
cd 'code_figure_table\OA\Figure OA22';
zindex=find(gridz==z);
V_high=squeeze(V_ini(1,:,zindex,:,:));
gb_high=squeeze(gb_ini(:,zindex,:,:));
gtheta_high=squeeze(gtheta_ini(:,zindex,:,:));
[V_high_not_mig, V_high_mig]=value(V_high, gb_high, gtheta_high, gridb, gridtheta, nb, ntheta, departure, destination, kappa);
eta_high=eta_ini;

theta=0.8;
theta_index=find(gridtheta==theta);
bindex=1:1:nb;

x1=gridb(bindex)';
y1=squeeze(V_low_mig(bindex,theta_index));
x2=gridb(bindex)';
y2=squeeze(V_mid_mig(bindex,theta_index));
x3=gridb(bindex)';
y3=squeeze(V_high_mig(bindex,theta_index));

%% draw
subplot(2,3,4);
plot(x1,y1,'-b','linewidth',2);
hold on
plot(gridb(bindex),squeeze(V_low_not_mig(bindex,theta_index)),'--k','linewidth',2);
hold on
plot(x1,y1-eta_low/0.05*0.1831,'--b','linewidth',1);
hold on
plot(x1,y1+eta_low/0.05*0.1831,'--b','linewidth',1);
title('D. Markets without branches, $\eta=0$', 'FontSize',15,'Interpreter','Latex');
xlabel('total wealth $m_t+a_t$', 'FontSize',12,'Interpreter','Latex');
ylabel('household''s value at $t+1$', 'FontSize',12,'Interpreter','Latex');
xlim(x);
xticks(5:1:8)
%ylim([-7,-4])
yticks(-7:1:-4)
set(gca,'FontSize',12);

subplot(2,3,5);
plot(x2,y2,'-b','linewidth',2);
hold on
plot(x2,y2-eta_mid/0.05*0.1831,'--b','linewidth',1);
hold on
plot(x2,y2+eta_mid/0.05*0.1831,'--b','linewidth',1);
hold on
plot(gridb(bindex),squeeze(V_mid_not_mig(bindex,theta_index)),'--k','linewidth',2);
title('E. Markets without branches, $\eta=0.1$', 'FontSize',15,'Interpreter','Latex');
xlabel('total wealth $m_t+a_t$', 'FontSize',12,'Interpreter','Latex');
ylabel('household''s value at $t+1$', 'FontSize',12,'Interpreter','Latex');
xlim(x);
xticks(5:1:8)
%ylim([-7,-4])
yticks(-7:1:-4)
set(gca,'FontSize',12);

subplot(2,3,6);
plot(x3,y3,'-b','linewidth',2);
hold on
plot(x3,y3-eta_high/0.05*0.1831,'--b','linewidth',1);
hold on
plot(x3,y3+eta_high/0.05*0.1831,'--b','linewidth',1);
hold on
plot(gridb(bindex),squeeze(V_high_not_mig(bindex,theta_index)),'--k','linewidth',2);
title('F. Markets without branches, $\eta=0.2$', 'FontSize',15,'Interpreter','Latex');
xlabel('total wealth $m_t+a_t$', 'FontSize',12,'Interpreter','Latex');
ylabel('household''s value at $t+1$', 'FontSize',12,'Interpreter','Latex');
xlim(x);
xticks(5:1:8)
%ylim([-7,-4])
yticks(-7:1:-4)
set(gca,'FontSize',12);